package cn.bdqn.service.impl;

import cn.bdqn.domain.Bunk;
import cn.bdqn.domain.Hospitalized;
import cn.bdqn.domain.Menber;
import cn.bdqn.mapper.HospitalizedMapper;
import cn.bdqn.service.BunkService;
import cn.bdqn.service.HospitalizedService;
import cn.bdqn.service.MenberService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class HospitalizedServiceImpl implements HospitalizedService {


    @Resource
    private HospitalizedMapper hospitalizedMapper;


    @Resource
    private MenberService menberService;

    @Resource
    private BunkService bunkService;

    @Resource
    private AmqpTemplate amqpTemplate;


    /**
     * 保存一条 住院记录
     *
     * @param bunkId
     * @param registrationId 挂号人id 身份证号
     * @param recipeId
     */
    @Transactional
    @Override
    public void save(Integer bunkId, String registrationId, String recipeId, Integer floodId) {
        Hospitalized hospitalized = new Hospitalized();
        hospitalized.setBunkId(bunkId);//  床位id
        hospitalized.setRegistrationId(registrationId); //挂号人id
        hospitalized.setRecipeId(recipeId); // 处方id
        hospitalized.setStatus(1);//住院
        hospitalized.setHospStart(new Date());//今天住院
        hospitalized.setHospDay(1);//住院时间 1天
        hospitalized.setHostEnd(null);//住院时间
        hospitalized.setPayStatus(0);//不结算
        hospitalized.setFloorId(floodId);//设置病区
        Menber menber = menberService.queryByMenberIdCard(registrationId);//获取病人信息
        System.out.println(menber);
        hospitalized.setName(menber.getMenberName());//设置病人名字
        Bunk bunk = bunkService.queryById(bunkId);//查询病床
        bunk.setPersonName(menber.getMenberName());//设置床位的病人信息
        bunk.setPersonPhone(menber.getMenberPhone());

        menber.setRoomNum(bunk.getBunkName()); //设置病人的房间号

//        BigDecimal oneDayMoney = hospitalizedMapper.selectOnedayMoneyByBunkName(bunk.getBunkName());

//        hospitalized.

        menberService.modifyById(menber);//更新病人信息
        bunkService.modifyPatientById(bunk);//给病人安排病房
        System.out.println(hospitalized);
        hospitalizedMapper.insertSelective(hospitalized);//保存住院记录
    }

    /**
     * 查询所有没出院的病人
     *
     * @return
     */
    @Override
    public List<Hospitalized> queryAllHospDayAndID() {
        return hospitalizedMapper.selectAllHospDayAndID();
    }

    @Transactional
    @Scheduled(cron = "0 00 12 * * ?")
    @Override
    /**
     * 定时任务 定时更新住院日期 每天的中午12点
     */
    public void modifyHospDayPlus1ById() {
        List<Hospitalized> hospitalizeds = queryAllHospDayAndID();
        List<Hospitalized> collect = hospitalizeds.stream().map(element -> {
            int hospDay = element.getHospDay();
            element.setHospDay(hospDay + 1);
            return element;
        }).collect(Collectors.toList());
        hospitalizedMapper.updateHospDayPlus1ById(collect);
    }

    /**
     * 出院
     *
     * @param id
     */
    @Transactional
    @Override
    public void modifyStatusByID(Integer id) {
        Hospitalized hospitalized = this.queryById(id);//查询住院信息
        hospitalized.setHostEnd(new Date());
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        start.setTime(hospitalized.getHospStart());
        end.setTime(hospitalized.getHostEnd());
        int differDayValue = Math.toIntExact((end.getTimeInMillis() - start.getTimeInMillis()) / (1000 * 60 * 60 * 24));//相差多少天
        hospitalized.setCountDay(differDayValue == 0 ? 1 : differDayValue);//设置住院天数
        System.out.println(hospitalized);
        int expenses = hospitalized.getFloor().getOnedayMoney().intValue() * hospitalized.getCountDay();//住院总费用
        hospitalized.setHostMoney((double) expenses);
        bunkService.modifyPersonNameAndPersonPhoneById(hospitalized.getBunkId());
        hospitalizedMapper.updateByID(hospitalized);
        amqpTemplate.convertAndSend("", "HM-Queue", hospitalized);
    }

    @Override
    public List<Hospitalized> queryByNameAndRegistrationId(String name, String registrationId) {
        return hospitalizedMapper.selectByNameAndRegistrationId(name, registrationId);
    }

    @Override
    public Hospitalized queryById(Integer id) {
        return hospitalizedMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Hospitalized> queryHospitalizedList() {
        return hospitalizedMapper.selectAll();
    }

    @Override
    public List<Hospitalized> queryHospitalizedByRegistrationIdAndName(String registrationId, String name) {
        return hospitalizedMapper.selectHospitalizedByRegistrationIdAndName(registrationId, name);
    }
}
